#include "wt588f_list.h"
#include "wt588f_board.h"

#if _VOICE_PLAY_TYPE_ == _VOICE_PLAY_SPEAKER_

AudioListNode_t AudioListCreate(void)
{
  AudioListNode_t AudioListNode = (AudioListNode_t)Wt588Malloc(sizeof(AudioTable_t));
  if (AudioListNode != NULL)
  {
    memset(AudioListNode, 0, sizeof(AudioTable_t));

    AudioListNode->NextItem = AudioListNode;
    AudioListNode->PreviousItem = AudioListNode;
  }

  return AudioListNode;
}

void AudioListDestroy(AudioListNode_t HeadNode)
{
  if (HeadNode != NULL)
  {
    AudioListNode_t CurrectNode = HeadNode->NextItem;
    while (CurrectNode != HeadNode)
    {
      AudioListNode_t TmpNoded = CurrectNode->NextItem;
      Wt588fFree(CurrectNode);

      CurrectNode = TmpNoded;
    }

    Wt588fFree(HeadNode);
  }
}

AudioListNode_t AudioListFind(AudioListNode_t HeadNode, uint32_t TriggerTime)
{
  if (HeadNode != NULL)
  {
    AudioListNode_t CurrectNode = HeadNode->NextItem;
    while (CurrectNode != HeadNode)
    {
      if (CurrectNode->FileTable.TriggerTime == TriggerTime)
        return CurrectNode;

      CurrectNode = CurrectNode->NextItem;
    }
  }

  return NULL;
}

AudioListNode_t AudioBuyNode(AudioFileTable_t *pFileTable)
{
  AudioListNode_t Node = NULL;

  if (pFileTable != NULL)
  {
    Node = (AudioListNode_t)Wt588Malloc(sizeof(AudioTable_t));
    if (Node != NULL)
    {
      memset(Node, 0, sizeof(AudioTable_t));

      memcpy(&Node->FileTable, pFileTable, sizeof(AudioFileTable_t));
      Node->NextItem = NULL;
      Node->PreviousItem = NULL;
    }
  }

  return Node;
}

void AudioInsertionNode(AudioListNode_t Pos, AudioFileTable_t *pFileTable)
{
  if ((Pos != NULL) && (pFileTable != NULL))
  {
    AudioListNode_t NewNode = AudioBuyNode(pFileTable);

    if (NewNode != NULL)
    {
      AudioListNode_t Parent = Pos->PreviousItem;

      Parent->NextItem = NewNode;
      NewNode->PreviousItem = Parent;
      NewNode->NextItem = Pos;
      Pos->PreviousItem = NewNode;
    }
  }
}

void AudioListErase(AudioListNode_t Pos)
{
  if (Pos != NULL)
  {
    AudioListNode_t PosPrev = Pos->PreviousItem;
    AudioListNode_t PosNext = Pos->NextItem;

    PosPrev->NextItem = PosNext;
    PosNext->PreviousItem = PosPrev;
    Wt588fFree(Pos);
  }
}

void AudioListPushBack(AudioListNode_t HeadNode, AudioFileTable_t *pFileTable)
{
  if (HeadNode != NULL)
  {
    AudioListNode_t NewNode = AudioBuyNode(pFileTable);

    if (NewNode != NULL)
    {
      HeadNode->PreviousItem->NextItem = NewNode;
      NewNode->PreviousItem = HeadNode->PreviousItem;
      NewNode->NextItem = HeadNode;
      HeadNode->PreviousItem = NewNode;
    }
  }
}

void AudioListPopBack(AudioListNode_t HeadNode)
{
  if (HeadNode != NULL)
  {
    if (HeadNode->PreviousItem != HeadNode)
    {
      AudioListNode_t TmpNode = HeadNode->PreviousItem;

      HeadNode->PreviousItem = TmpNode->PreviousItem;
      TmpNode->PreviousItem->NextItem = HeadNode;
      Wt588fFree(TmpNode);
      TmpNode = NULL;
    }
  }
}

void AudioListPushFront(AudioListNode_t HeadNode, AudioFileTable_t *pFileTable)
{
  if ((HeadNode != NULL) && (pFileTable != NULL))
  {
    AudioListNode_t NewNode = AudioBuyNode(pFileTable);

    if (NewNode != NULL)
    {
      NewNode->NextItem = HeadNode->NextItem;
      NewNode->PreviousItem = HeadNode;
      HeadNode->NextItem->PreviousItem = NewNode;
      HeadNode->NextItem = NewNode;
    }
  }
}

void AudioListPopFront(AudioListNode_t HeadNode)
{
  if (HeadNode->NextItem != HeadNode)
  {
    AudioListNode_t Node = HeadNode->NextItem;

    Node->NextItem->PreviousItem = HeadNode;
    HeadNode->NextItem = Node->NextItem;
    Wt588fFree(Node);
    Node = NULL;
  }
}

#endif
